package shajie.shajun.mmhf.View.Fragment;

import android.content.DialogInterface;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import shajie.shajun.mmhf.Adapter.MulRecyclerAdapter;
import shajie.shajun.mmhf.Base.BaseFragment;
import shajie.shajun.mmhf.Bean.Beans;
import shajie.shajun.mmhf.Bean2.OutGoods;
import shajie.shajun.mmhf.Bean2.Result;
import shajie.shajun.mmhf.Interface.RecyclerItemClickListener;
import shajie.shajun.mmhf.R;
import shajie.shajun.mmhf.ViewModel.Bmobdata;
import shajie.shajun.mmhf.databinding.FragmentOutgoosBinding;

/**
 * Created by admin on 2018/1/27.
 */

public class OutGoods_Fragment extends BaseFragment<FragmentOutgoosBinding> {
    private FragmentOutgoosBinding mBinding;
    public MulRecyclerAdapter<OutGoods> mulRecyclerAdapter;
    @Override
    public int setlayoutid() {
        return R.layout.fragment_outgoos;
    }

    @Override
    public void init(FragmentOutgoosBinding binding) {
        this.mBinding=binding;
        LinearLayoutManager layoutManager=new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        binding.recOutgoods.setLayoutManager(layoutManager);
        refreshFruits();
        mBinding.outgoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().beginTransaction()
                        .addToBackStack(null)
                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                        .add(R.id.container,AddOutGoods_Frgment.NewInten(new OutGoods()),"AddOutGoods_Frgment")
                        .commit();

            }
        });
        mBinding.btnClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getContext())
                    .setTitle("确认")
                    .setMessage("确定清算邮费妈？")
                    .setCancelable(true)
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Bmobdata.getpicture(getContext(), "ClearPostPrice", new Bmobdata.Singer<Result>() {
                                @Override
                                public void success(Result s) {
                                    if (s.getResult().contains("rue")) {
                                        Toast.makeText(getContext(), "清算成功！", Toast.LENGTH_SHORT).show();
                                        mBinding.tvPostprice.setText("总邮费:0.0");
                                        mBinding.btnClean.setEnabled(false);
                                        mulRecyclerAdapter.clearoutgoodsdata();

                                    }
                                }

                                @Override
                                public void faile(String s) {

                                }
                            });
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.
                            OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })
                    .show();


            }
        });
        mBinding.swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFruits();
            }
        });
        mBinding.recOutgoods.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                getFragmentManager().beginTransaction()
                        .addToBackStack(null)
                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                        .add(R.id.container, AddOutGoods_Frgment.NewInten(mulRecyclerAdapter.getOutGoods(position)),"AddOutGoods_Frgment")
                        .commit();
            }

            @Override
            public void onLongClick(View view, final int posotion) {
                new AlertDialog.Builder(getContext())
                        .setTitle("删除")
                        .setMessage("确定要删除该项吗？")
                        .setCancelable(true)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Map<String,String> map = new HashMap<>();
                                map.put("id","DeleteConsignment");
                                map.put("Key",""+mulRecyclerAdapter.getOutGoods(posotion).getID());
                                Bmobdata.addOutGoods(getContext(), map, new Bmobdata.Singer<Result>() {
                                    @Override
                                    public void success(Result s) {
                                        if (s.getResult().contains("rue")) {
                                            toast("删除成功");
                                            if (mulRecyclerAdapter.getOutGoods(posotion).getClear().contains("alse")){

                                                String reg = "[\\d\\.]+";
                                                Pattern pattern = Pattern.compile(reg);
                                                Matcher matcher =pattern.matcher(mBinding.tvPostprice.getText().toString());
                                                //toast(mBinding.tvPostprice.getText().toString());
                                                matcher.find();
                                                double n = Double.valueOf(mBinding.tvPostprice.getText().toString().substring(matcher.start(),matcher.end())).doubleValue();
                                                toast(n+"");
                                                if (n>0){
                                                    mBinding.tvPostprice.setText("总邮费:"+String.valueOf(n-mulRecyclerAdapter.getOutGoods(posotion).getPostPrice()));
                                                }
                                            }
                                            mulRecyclerAdapter.deleteOutGoods(posotion);
                                        }
                                    }

                                    @Override
                                    public void faile(String s) {

                                    }
                                });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.
                                OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .show();

            }
        }));
    }
    public void refreshFruits(){
        Bmobdata.OutGoods(getContext(), new Bmobdata.Singer<List<OutGoods>>() {
            @Override
            public void success(List<OutGoods> s) {
                Beans<OutGoods> beans=new Beans<>(R.layout.item_outgoos,s);
                mulRecyclerAdapter=new MulRecyclerAdapter<>(getContext(),beans);
                mBinding.recOutgoods.setAdapter(mulRecyclerAdapter);
                double n=0.0f;
                for (int i = 0; i < s.size(); i++) {
                    if (s.get(i).getClear().equals("False")){
                        n+=s.get(i).getPostPrice();
                    }
                }
                if (n>0.0f){
                    mBinding.btnClean.setEnabled(true);
                }
                mBinding.tvPostprice.setText("总邮费:"+n);
                mBinding.swipeRefresh.setRefreshing(false);
            }

            @Override
            public void faile(String s) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
